package com.huajx.armp.htxny.codec;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolEncoder;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;

/**
 * @program: di-parent
 * @description:
 * @author: lisa 叶新华
 * @create: 2020-03-25 16:38
 */
public class HtxnyEncoder implements ProtocolEncoder {
    @Override
    public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception {
        if (message == null) {
            return;
        }

        // 消息类型判断处理
        if (message instanceof String) {
            out.write(IoBuffer.wrap(((String) message).getBytes()));
        } else if (message instanceof byte[]) {
            out.write(IoBuffer.wrap((byte[]) message));
        } else if(message instanceof StringBuilder) {
            out.write(IoBuffer.wrap(((StringBuilder)message).toString().getBytes()));
        } else if(message instanceof StringBuffer) {
            out.write(IoBuffer.wrap(((StringBuffer)message).toString().getBytes()));
        }
    }

    @Override
    public void dispose(IoSession session) throws Exception {

    }
}
